package com.sxhyll.web.tags;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;

import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.sxhyll.web.tags.component.Paginate;


import freemarker.template.Template;
import freemarker.template.TemplateException;

@Component
@SuppressWarnings("serial")
public class PaginateTag extends RequestContextAwareTag {

	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	private String nextLabel;
	private String previousLabel;
	private JspWriter out;
	private String url;
	

	@Override
	protected int doStartTagInternal() throws Exception {
		initData();
		Paginate paginate = (Paginate) pageContext.findAttribute("paginate");
		
		adjustPageNo(paginate);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paginate", paginate);
		map.put("nextLabel", nextLabel);
		map.put("previousLabel", previousLabel);
		map.put("url", url);
		Template t =  freeMarkerConfigurer.getConfiguration().getTemplate("pagenate.ftl");
		try {
			String template = FreeMarkerTemplateUtils.processTemplateIntoString(t, map);
			out.print(template);
		} catch (TemplateException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private void initData(){
		freeMarkerConfigurer = (FreeMarkerConfigurer) getRequestContext().getWebApplicationContext().getBean("freeMarkerConfigurer");
		nextLabel = getRequestContext().getMessage("paginate.nextLabel");
		previousLabel = getRequestContext().getMessage("paginate.previousLabel");
		out = pageContext.getOut();
		url = getURL();
	}
	
	private String getURL(){
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String baseUrl = (String)request.getAttribute("javax.servlet.forward.request_uri");
		if(baseUrl == null) baseUrl = (String) request.getRequestURI();
		String q = getQueryString(request);
		return baseUrl + q;
	}
	
	private String getQueryString(HttpServletRequest request){
		StringBuffer q = new StringBuffer();
		Map<String, String[]>params = request.getParameterMap();
		q.append("?");
		for(String key : params.keySet()){
			if(key.equalsIgnoreCase("pageNo")){
				continue;
			}
			String [] values = params.get(key);
			for(int i = 0; i< values.length; i++) {
				q.append(key);
				q.append("=");
				q.append(values[i]);
				q.append("&");
			}
		}
		q.append("pageNo=<1>");
		return q.toString();
	}
	private void adjustPageNo(Paginate paginate){
		if(paginate.getPageNo() > paginate.getTotalPage()){
			paginate.setPageNo(paginate.getTotalPage());
		}
		if(paginate.getPageNo() <= 0){
			paginate.setPageNo(1);
		}
	}
	public static void main(String [] args){
	}
}
